#include "hsf.h"
#include "mqttlib.h"
#include "m2m_socket.h"
#include "airkissv2.h"

int g_module_id = __HF_MODULE_ID__;

#if (__HF_MODULE_ID__==HFM_LPB120)

const int hf_gpio_fid_to_pid_map_table[HFM_MAX_FUNC_CODE]=
{
	HFM_NOPIN,	//HFGPIO_F_JTAG_TCK
	HFM_NOPIN,	//HFGPIO_F_JTAG_TDO
	HFM_NOPIN,	//HFGPIO_F_JTAG_TDI
	HFM_NOPIN,	//HFGPIO_F_JTAG_TMS
	HFM_NOPIN,	//HFGPIO_F_USBDP
	HFM_NOPIN,	//HFGPIO_F_USBDM
	HFM_GPIO20,	//HFGPIO_F_UART0_TX
	HFM_NOPIN,	//HFGPIO_F_UART0_RTS
	HFM_GPIO19,	//HFGPIO_F_UART0_RX
	HFM_NOPIN,	//HFGPIO_F_UART0_CTS
	
	HFM_NOPIN,	//HFGPIO_F_SPI_MISO
	HFM_NOPIN,	//HFGPIO_F_SPI_CLK
	HFM_NOPIN,	//HFGPIO_F_SPI_CS
	HFM_NOPIN,	//HFGPIO_F_SPI_MOSI
	
	HFM_GPIO5,	//HFGPIO_F_UART1_TX,
	HFM_NOPIN,	//HFGPIO_F_UART1_RTS,
	HFM_GPIO6,	//HFGPIO_F_UART1_RX,
	HFM_NOPIN,	//HFGPIO_F_UART1_CTS,
	
	HFM_GPIO15,	//HFGPIO_F_NLINK
	HFM_GPIO18,	//HFGPIO_F_NREADY
	HFM_GPIO2,	//HFGPIO_F_NRELOAD
	HFM_NOPIN,		//HFGPIO_F_SLEEP_RQ
	HFM_NOPIN,		//HFGPIO_F_SLEEP_ON
	
	HFM_NOPIN,		//HFGPIO_F_WPS
	HFM_NOPIN,		//HFGPIO_F_RESERVE1
	HFM_NOPIN,		//HFGPIO_F_RESERVE2
	HFM_NOPIN,		//HFGPIO_F_RESERVE3
	HFM_NOPIN,		//HFGPIO_F_RESERVE4
	HFM_NOPIN,		//HFGPIO_F_RESERVE5
	
	HFM_NOPIN,	//HFGPIO_F_USER_DEFINE
};

#elif  (__HF_MODULE_ID__==HFM_LPT220)

const int hf_gpio_fid_to_pid_map_table[HFM_MAX_FUNC_CODE]=
{
	HFM_NOPIN,	//HFGPIO_F_JTAG_TCK
	HFM_NOPIN,	//HFGPIO_F_JTAG_TDO
	HFM_NOPIN,	//HFGPIO_F_JTAG_TDI
	HFM_NOPIN,	//HFGPIO_F_JTAG_TMS
	HFM_NOPIN,		//HFGPIO_F_USBDP
	HFM_NOPIN,		//HFGPIO_F_USBDM
	HFM_GPIO20,	//HFGPIO_F_UART0_TX
	HFM_NOPIN,	//HFGPIO_F_UART0_RTS
	HFM_GPIO19,	//HFGPIO_F_UART0_RX
	HFM_NOPIN,	//HFGPIO_F_UART0_CTS
	
	HFM_NOPIN,	//HFGPIO_F_SPI_MISO
	HFM_NOPIN,	//HFGPIO_F_SPI_CLK
	HFM_NOPIN,	//HFGPIO_F_SPI_CS
	HFM_NOPIN,	//HFGPIO_F_SPI_MOSI
	
	HFM_GPIO5,	//HFGPIO_F_UART1_TX,
	HFM_NOPIN,	//HFGPIO_F_UART1_RTS,
	HFM_GPIO6,	//HFGPIO_F_UART1_RX,
	HFM_NOPIN,	//HFGPIO_F_UART1_CTS,
	
	HFM_GPIO15,	//HFGPIO_F_NLINK
	HFM_GPIO3,	//HFGPIO_F_NREADY
	HFM_GPIO2,	//HFGPIO_F_NRELOAD
	HFM_NOPIN,		//HFGPIO_F_SLEEP_RQ
	HFM_NOPIN,		//HFGPIO_F_SLEEP_ON
	
	HFM_NOPIN,		//HFGPIO_F_WPS
	HFM_NOPIN,		//HFGPIO_F_RESERVE1
	HFM_NOPIN,		//HFGPIO_F_RESERVE2
	HFM_NOPIN,		//HFGPIO_F_RESERVE3
	HFM_NOPIN,		//HFGPIO_F_RESERVE4
	HFM_NOPIN,		//HFGPIO_F_RESERVE5
	
	HFM_NOPIN,	//HFGPIO_F_USER_DEFINE
};

#elif (__HF_MODULE_ID__ == HFM_LPT120)

const int hf_gpio_fid_to_pid_map_table[HFM_MAX_FUNC_CODE]=
{
	HFM_NOPIN,	//HFGPIO_F_JTAG_TCK
	HFM_NOPIN,	//HFGPIO_F_JTAG_TDO
	HFM_NOPIN,	//HFGPIO_F_JTAG_TDI
	HFM_NOPIN,	//HFGPIO_F_JTAG_TMS
	HFM_NOPIN,		//HFGPIO_F_USBDP
	HFM_NOPIN,		//HFGPIO_F_USBDM
	HFM_GPIO20,	//HFGPIO_F_UART0_TX
	HFM_NOPIN,	//HFGPIO_F_UART0_RTS
	HFM_GPIO19,	//HFGPIO_F_UART0_RX
	HFM_NOPIN,	//HFGPIO_F_UART0_CTS
	
	HFM_NOPIN,	//HFGPIO_F_SPI_MISO
	HFM_NOPIN,	//HFGPIO_F_SPI_CLK
	HFM_NOPIN,	//HFGPIO_F_SPI_CS
	HFM_NOPIN,	//HFGPIO_F_SPI_MOSI
	
	HFM_GPIO5,	//HFGPIO_F_UART1_TX,
	HFM_NOPIN,	//HFGPIO_F_UART1_RTS,
	HFM_GPIO6,	//HFGPIO_F_UART1_RX,
	HFM_NOPIN,	//HFGPIO_F_UART1_CTS,
	
	HFM_GPIO15,	//HFGPIO_F_NLINK
	HFM_GPIO3,	//HFGPIO_F_NREADY
	HFM_GPIO2,	//HFGPIO_F_NRELOAD
	HFM_NOPIN,		//HFGPIO_F_SLEEP_RQ
	HFM_NOPIN,		//HFGPIO_F_SLEEP_ON
	
	HFM_NOPIN,		//HFGPIO_F_WPS
	HFM_NOPIN,		//HFGPIO_F_RESERVE1
	HFM_NOPIN,		//HFGPIO_F_RESERVE2
	HFM_NOPIN,		//HFGPIO_F_RESERVE3
	HFM_NOPIN,		//HFGPIO_F_RESERVE4
	HFM_NOPIN,		//HFGPIO_F_RESERVE5
	
	HFM_NOPIN,	//HFGPIO_F_USER_DEFINE
};
#else
#error "Please define __HF_MODULE_ID__!!!"
#endif

//#define SUPPORT_UART_THROUGH
#define TEST_TIMER_ID		(1)


#if  defined(ID_JIABANG)

	#define DEVICE_TYPE 	"gh_925b31311edd"
	#define DEVICE_ID		"39887"

#elif   defined(ID_ZEFENG)

	#define DEVICE_TYPE 	"gh_d08dafa2fe90"
	#define DEVICE_ID		"40275"

#elif   defined(ID_DIJIA)
	
	#define DEVICE_TYPE 	"gh_2b4f98109785"
	#define DEVICE_ID		"41403"

#elif   defined(ID_WX)
	
	#define DEVICE_TYPE 	"gh_d799cdf621c5"
	#define DEVICE_ID		"34036"
 
#elif   defined(ID_GEEK)

#define DEVICE_TYPE 	"gh_7b1c0c4ab022"
#define DEVICE_ID		"34057"

#elif   defined(ID_LJN)

#define DEVICE_TYPE 	"gh_4e4a01c22736"
#define DEVICE_ID		"42869"

#elif   defined(ID_JINGGE)

#define DEVICE_TYPE 	"gh_57481b1d744c"
#define DEVICE_ID		"43322"

#else   
 
	#define DEVICE_TYPE 	"gh_d799cdf621c5"
	#define DEVICE_ID		"34036"

#endif




char get_mac[12]={'0'};

uip_ipaddr_t remote_addr_report={255,255,255,255};
unsigned short port_report=20000;
NETSOCKET u_socket_report;

void udp_airkiss_start(void);
void udp_report_start(void);

const hfat_cmd_t user_define_at_cmds_table[]=
{
	{NULL,NULL,NULL,NULL}
};

extern void mqtt_socket_start(void);
extern mqtt_broker_handle_t g_stMQTTBroker;
extern topic_para *g_pub_topic ;
extern topic_para *g_sub_topic ;
extern char sub_topic_name[64];
extern char pub_topic_name[64];

extern void tcpserver_test_start(void);



uint8_t dhcp_ip[4];
uint8_t sta_mac[6]={0};


uint8_t mqtt_establish_ok=0;

#if defined(WX_TOPIC)
	uint8_t wifi_state_no[18]={0xaa,0x12,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x02,0x00,0x00,0x00,0x12};
	uint8_t wifi_state_router[18]={0xaa,0x12,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x02,0x00,0x00,0x01,0x11};
	uint8_t wifi_state_cloud[18]={0xaa,0x12,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x02,0x00,0x00,0x02,0x10};			
#elif defined(ORKAN_TOPIC)||defined(ORKAN_TOPIC_XF_V1_1)||defined(JINGGE_TOPIC)||defined(LJN_TOPIC) ||defined(DIJIA_TOPIC)||defined(ZEFENG_TOPIC)||defined(JIABANG_TOPIC)||defined(ZHONGQING_TOPIC)		
	uint8_t wifi_state_cloud[13]={0xaa,0x55,0xaa,0x55,0x00,0x01,0x00,0x10,0x40,0xbf,0x00,0x00,0x00};
	uint8_t wifi_state_router[13]={0xaa,0x55,0xaa,0x55,0x00,0x01,0x00,0x10,0x80,0x7e,0x00,0x00,0x01};
	uint8_t wifi_state_no[13]={0xaa,0x55,0xaa,0x55,0x00,0x01,0x00,0x10,0x81,0x3e,0x00,0x00,0x02};  
#endif

#if defined(WX_TOPIC) 
	uint8_t beatHeart0[24]={0xaa,0x18,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x11,0x15,0x00,0x00,0x00};
	uint8_t beatHeart1[24]={0xaa,0x18,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x11,0x15,0x00,0x00,0x01};	
	uint8_t beatHeart2[24]={0xaa,0x18,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x11,0x15,0x00,0x00,0x02};	
#elif defined(ORKAN_TOPIC)||defined(ORKAN_TOPIC_XF_V1_1)||defined(JINGGE_TOPIC)||defined(LJN_TOPIC) ||defined(DIJIA_TOPIC)||defined(ZEFENG_TOPIC)||defined(JIABANG_TOPIC)||defined(ZHONGQING_TOPIC)		
	uint8_t beatHeart0[19]={0xaa,0x55,0xaa,0x55,0x00,0x07,0x00,0x0f,0xff,0xff,0x00,0x00,0x00};    
	uint8_t beatHeart1[19]={0xaa,0x55,0xaa,0x55,0x00,0x07,0x00,0x0f,0xff,0xff,0x00,0x00,0x01};  
	uint8_t beatHeart2[19]={0xaa,0x55,0xaa,0x55,0x00,0x07,0x00,0x0f,0xff,0xff,0x00,0x00,0x02};  		
#endif




uint16_t mac_crc=0;
uint8_t udp_mac_data[256];
uint8_t wifi_State=0; 
uint8_t crc_dt=0;
uint8_t heartbeat_count=0;

uint16_t yc_crc_cal(uint8_t *data_to_cal,uint8_t data_len)
{
	int i=0;
	uint16_t crc_value=0xffff;
	while(data_len--)
	{
	  crc_value^=*data_to_cal++;
    for(i=0;i<8;i++)
		{
		  if(crc_value&0x0001) crc_value=(crc_value>>1)^0xa001;
		  else crc_value=crc_value>>1;
		}
	}
	return (crc_value);
}

void proChecksumGen(uint8_t * pro_buff)
{
	int i;
	crc_dt=0;
	for(i=0;i<pro_buff[1]-1;i++)
	{
		crc_dt+=pro_buff[i];
	}
	crc_dt=~crc_dt;
	crc_dt+=1;
}

uint8_t proChecksumGen1(uint8_t * pro_buff)
{
	int i;
	uint8_t crc_tmp=0;
	for(i=0;i<pro_buff[1]-1;i++)
	{
		crc_tmp+=pro_buff[i];
	}
	crc_tmp=~crc_tmp;
	crc_tmp+=1;
	return crc_tmp;
}

void HexStrToByte(const char* source, unsigned char* dest, int sourceLen)  
{  
    short i;  
    unsigned char highByte, lowByte;  
      
    for (i = 0; i < sourceLen; i += 2)  
    {  
        highByte = toupper(source[i]);  
        lowByte  = toupper(source[i + 1]);  
        if (highByte > 0x39)  
            highByte -= 0x37;  
        else  
            highByte -= 0x30;  
        if (lowByte > 0x39)  
            lowByte -= 0x37;  
        else  
            lowByte -= 0x30;  
        dest[i / 2] = (highByte << 4) | lowByte;  
    }  
    return ;  
}  
/// <summary>
/// 获取模块的mac地址
/// </summary>
int get_sta_mac(void)
{
	int i=0;
    char *words[6]={NULL};
	char rsp[64]={0};
	hfat_send_cmd("AT+WSMAC\r\n",sizeof("AT+WSMAC\r\n"),rsp,64);
	if(hfat_get_words(rsp,words,3)>0)
	{		
		if(0==strncmp(words[0],"+ok",strlen("+ok")))
		{
		  words[1]=strlwr(words[1]);
		  strcpy(get_mac,words[1]);
		  #if defined(_sys_debug_)
		  	u_printf("get_mac=====================%s\n",get_mac);
			u_printf("get_mac--------");
			for(i=0;i<12;i++)  u_printf("%02x ",get_mac[i]); 
			u_printf("\r\n");			
		  #endif
		  HexStrToByte(get_mac,sta_mac,12);
		  #if defined(ORKAN_TOPIC) ||defined(ORKAN_TOPIC_XF_V1_1)||defined(JINGGE_TOPIC)||defined(DIJIA_TOPIC) ||defined(LJN_TOPIC)||defined(ZEFENG_TOPIC)||defined(JIABANG_TOPIC)||defined(ZHONGQING_TOPIC)			
		  memcpy(beatHeart0+13,sta_mac,6); 	
		  memcpy(beatHeart1+13,sta_mac,6); 
		  memcpy(beatHeart2+13,sta_mac,6); 
		  mac_crc=yc_crc_cal(beatHeart0+12,7);
		  beatHeart0[8]=mac_crc>>8;
		  beatHeart0[9]=mac_crc>>0;
		  mac_crc=yc_crc_cal(beatHeart1+12,7);
		  beatHeart1[8]=mac_crc>>8;
		  beatHeart1[9]=mac_crc>>0;	
		  mac_crc=yc_crc_cal(beatHeart2+12,7);
		  beatHeart2[8]=mac_crc>>8;
		  beatHeart2[9]=mac_crc>>0;			  	  
		  #elif defined(WX_TOPIC)
			memcpy(beatHeart0+17,sta_mac,6); 
			memcpy(beatHeart1+17,sta_mac,6); 	
			memcpy(beatHeart2+17,sta_mac,6);
            proChecksumGen(beatHeart0);	
			beatHeart0[23]=crc_dt;
			#if defined(_sys_debug_)
				u_printf("beatHeart0[23]==%d\n",crc_dt);
		    #endif				
            proChecksumGen(beatHeart1);	
			beatHeart1[23]=crc_dt;
			#if defined(_sys_debug_)
				u_printf("beatHeart1[23]==%d\n",crc_dt);
		    #endif							
            proChecksumGen(beatHeart2);	
			beatHeart2[23]=crc_dt;	
			#if defined(_sys_debug_)
			u_printf("beatHeart2[23]==%d\n",crc_dt);
		    #endif													
		  #endif	
		  memcpy(udp_mac_data,sta_mac,6); 	  
		  return 0;
		}
	}	
	return -1;	
}

uint32_t dhcp_tst=0;

extern void mqtt_socket_exit(void);

static int hfsys_event_callback(uint32_t event_id,void * param)
{ 
    switch(event_id)
    {
	case HFE_WIFI_STA_CONNECTED:
		#if defined(_sys_debug_)
			u_printf("WIFI STATE=====HFE_WIFI_STA_CONNECTED-----------\n");
	    #endif	
        break;
	case HFE_WIFI_STA_DISCONNECTED:
		#if defined(_sys_debug_)
	    	u_printf("WIFI STATE=====HFE_WIFI_STA_DISCONNECTED\n");
		#endif	
		hfuart_send(HFUART0,wifi_state_no,sizeof(wifi_state_no),100);
		wifi_State=0;
        break;
    case HFE_DHCP_OK:
		memcpy(dhcp_ip,param,sizeof(dhcp_ip));
		#if defined(_sys_debug_)
			u_printf("WIFI STATE=====HFE_DHCP_OK\n");
			u_printf("timetest-----------start_to_connect_server\n");
		#endif			
		hfuart_send(HFUART0,wifi_state_router,sizeof(wifi_state_router),100);	
		mqtt_socket_start();	
		#if defined(_sys_debug_)
			dhcp_tst++;	
		#endif	
		if(wifi_State!=0)  hfsys_reset();
		wifi_State=1;		
        break;
    case HFE_SMTLK_OK:
        break;
    case HFE_CONFIG_RELOAD:
		//hfuart_send(HFUART0,wifi_state_no,sizeof(wifi_state_no),100);
		//hfsys_reset();   
        break;
    default:
        break;
    }
	
    return 0;
}
/// <summary>
/// 设置工作模式
/// </summary>
int set_work_mode_ap(void)
{
	char *words[6]={NULL};
	char rsp[64]={0};
	hfat_send_cmd("AT+WMODE=AP\r\n",sizeof("AT+WMODE=AP\r\n"),rsp,64);
	if(hfat_get_words(rsp,words, 3)>0)
	{
		if((0==strncmp(words[0],"+ok",strlen("+ok"))))
		{
		  return 0;
		}
	}	
	return -1;	
}
/// <summary>
/// 设置工作模式
/// </summary>
int set_work_mode(void)
{
	char *words[6]={NULL};
	char rsp[64]={0};
	hfat_send_cmd("AT+WMODE=STA\r\n",sizeof("AT+WMODE=STA\r\n"),rsp,64);
	if(hfat_get_words(rsp,words, 3)>0)
	{
		if((0==strncmp(words[0],"+ok",strlen("+ok"))))
		{
		  return 0;
		}
	}	
	return -1;	
}

int loops;
uint16_t data_len_pro=0,data_crc_pro=0;
static int USER_FUNC uart_recv_callback(uint32_t event,char *data,uint32_t len,uint32_t buf_len)
{	
	#if defined(_sys_debug_)
   			u_printf("recv device data\r\n");
			for(loops=0;loops<len;loops++)  u_printf("%02x ",data[loops]); 
			u_printf("\r\n"); 
	#endif 
   #if defined(WX_TOPIC) 
		if(data[0]==0xaa&&data[1]<250)
		{
			if(data[data[1]-1]==proChecksumGen1(data))
			{
				if(data[12]==0x20&&data[13]==0x01)
				{

						hfuart_send(HFUART0,data,len,100);
						hfuart_send(HFUART0,data,len,100);
						hfsmtlk_start();
					
						#if defined(_sys_debug_)
							u_printf("recv the msg to connect to net\n");
						#endif         
					return len;
				}
				else if(data[12]==0x20&&data[13]==0x06)
				{
						hfuart_send(HFUART0,data,len,100);
						hfuart_send(HFUART0,data,len,100);
						
						hfsys_reload();
						#if defined(_sys_debug_)
							u_printf("recv the msg to reload module\n");
						#endif 
								
					return len;
				}
				else if(data[12]==0x20&&data[13]==0x07)
				{
						hfuart_send(HFUART0,data,len,100);
						hfuart_send(HFUART0,data,len,100);
						
						hfsys_reset();
						#if defined(_sys_debug_)
							u_printf("recv the msg to reset\n");
						#endif 
								
					return len;
				}	   	   
			}
		}
   #elif defined(ORKAN_TOPIC)||defined(ORKAN_TOPIC_XF_V1_1) ||defined(JINGGE_TOPIC)||defined(DIJIA_TOPIC)||defined(LJN_TOPIC)||defined(ZEFENG_TOPIC)||defined(JIABANG_TOPIC)||defined(ZHONGQING_TOPIC)
		
        if(data[0]==0xaa&&data[1]==0x55&&data[2]==0xaa&&data[3]==0x55)  
		{
			   if(data[6]==0x00&&data[7]==0x0a)
			   {
					data_len_pro =(uint16_t)data[4]*256+data[5];
					data_crc_pro =(uint16_t)data[8]*256+data[9];
					if(data_crc_pro==yc_crc_cal(data+12,data_len_pro))
					{
					   if(data[12]==0)
					   {
							hfuart_send(HFUART0,data,len,100);
							hfuart_send(HFUART0,data,len,100);
							hfsmtlk_start();
							#if defined(_sys_debug_)
								u_printf("recv the msg to connect to net\n");
							#endif                            
					   }
					   else if(data[12]==1)
					   {
							hfuart_send(HFUART0,data,len,100);
							hfuart_send(HFUART0,data,len,100);
							#if defined(_sys_debug_)
								u_printf("recv the msg to reload module\n");
							#endif 
							if(set_work_mode_ap()==0)
							{
                                hfsys_reset();
							}

					   }
					   else if(data[12]==2)
					   {
							hfuart_send(HFUART0,data,len,100);
							hfuart_send(HFUART0,data,len,100);
							hfsys_reset();
							#if defined(_sys_debug_)
								u_printf("recv the msg to reset\n");
							#endif 
					   }
					   else if(data[12]==3)
					   {
							hfuart_send(HFUART0,data,len,100);
							hfuart_send(HFUART0,data,len,100);
							#if defined(_sys_debug_)
								u_printf("recv the msg to sta mode\n");
							#endif 
							if(set_work_mode()==0)
							{
                                hfsys_reset();
							}							
					   }					   
					   return len;	
					}
			   }
		}   

   #endif





	//来自串口的数据通过udp和mqtt转发出去
	if(mqtt_establish_ok==1) 
	{
		if(1==mqtt_publish(&g_stMQTTBroker, g_pub_topic->name, (const char *)data,len, 0))
		{
			#if defined(_sys_debug_)
				u_printf("mqtt_publish send ok\n");
	    	#endif			
		}
		#if defined(_sys_debug_)
			u_printf("g_pub_topic->name===%s,pub_topic_name==%s\r\n",g_pub_topic->name,pub_topic_name);
			u_printf("g_sub_topic->name===%s,sub_topic_name==%s\r\n",g_sub_topic->name,sub_topic_name);
			u_printf("recv device data\r\n");
			for(loops=0;loops<len;loops++)  u_printf("%02x ",data[loops]); 
			u_printf("\r\n");
	    #endif					
	}
	if(u_socket_report>=0)
	{
		 if(len<240)  memcpy(udp_mac_data+6,data,len); 
		 hfnet_udp_sendto(u_socket_report,udp_mac_data,len+6,&remote_addr_report,port_report);
		 #if defined(_sys_debug_)
		 	u_printf("u_socket_report======%d\r\n",u_socket_report);
	     #endif		 	
	}
	return len;
}


static int USER_FUNC socketa_recv_callback(uint32_t event,char *data,uint32_t len,uint32_t buf_len)
{
	if(event==HFNET_SOCKETA_CONNECTED)
	{

	}
	else if(event==HFNET_SOCKETA_DISCONNECTED)
	{

	}
	else if(event==HFNET_SOCKETA_DATA_READY)
	{
		
	}
}

int looph=0;
static void test_timer_callback( hftimer_handle_t htimer )
{
	if(hftimer_get_timer_id(htimer)==TEST_TIMER_ID)
	{
			#if defined(_sys_debug_)
				u_printf("wifi_State======%d,mqtt_establish_ok====%d,get_mac==%s,dhcp_tst==%d\n",wifi_State,mqtt_establish_ok,get_mac,dhcp_tst);
			#endif

		    if(wifi_State==1) 
			{
				heartbeat_count++;
				if(heartbeat_count>10)
				{
					 hfsys_softreset();
				}
			   hfuart_send(HFUART0,beatHeart1,sizeof(beatHeart1),100);
			   #if defined(_sys_debug_)
					u_printf("heartbeat===");	
					for(looph=0;looph<sizeof(beatHeart1);looph++)  u_printf("%02x ",beatHeart1[looph]); 
					u_printf("\r\n");
		       #endif					   
			}
			else if(wifi_State==0)
			{
               hfuart_send(HFUART0,beatHeart0,sizeof(beatHeart0),100); 
			}
			else if(wifi_State==2)
			{	
				heartbeat_count++;
				if(heartbeat_count>10)
				{
					 hfsys_softreset();
				}
			   hfuart_send(HFUART0,beatHeart2,sizeof(beatHeart2),100);  
			   #if defined(_sys_debug_)
					u_printf("heartbeat===");	
					for(looph=0;looph<sizeof(beatHeart2);looph++)  u_printf("%02x ",beatHeart2[looph]); 
					u_printf("\r\n");
		       #endif				    			   
			} 
	}
}

static void test_timer_start(void)
{
	hftimer_handle_t test_timer;
	//创建一个自动定时器，每1s钟触发一次。
	if((test_timer = hftimer_create("TEST-TIMER",3000,true,TEST_TIMER_ID,test_timer_callback,0))==NULL)
	{
		//u_printf("create timer fail\n");
	}
	hftimer_start(test_timer);
}


static NETSOCKET app_udp_socket =-1;
uint8_t  lan_buf[200];
uint16_t lan_buf_len;

const airkiss_config_t akconf =
{
	(airkiss_memset_fn)&memset,
	(airkiss_memcpy_fn)&memcpy,
	(airkiss_memcmp_fn)&memcmp,
	0,
};


static int app_udp_recv_callback(NETSOCKET socket, 
	unsigned char *data, 
	unsigned short len,
	uip_ipaddr_t *peeraddr, 
	unsigned short peerport)
{
	airkiss_lan_ret_t ret = airkiss_lan_recv(data, len, &akconf);
	airkiss_lan_ret_t packret;	
    switch(ret)
	{
 		case AIRKISS_LAN_SSDP_REQ:

		    lan_buf_len =sizeof(lan_buf)  ;

			packret = airkiss_lan_pack(AIRKISS_LAN_SSDP_RESP_CMD,
						DEVICE_TYPE, get_mac, 0, 0, lan_buf, &lan_buf_len, &akconf);

			if (packret != AIRKISS_LAN_PAKE_READY) {
				#if defined(_sys_debug_)
					u_printf("Pack lan packet error!!!!!!!\n");
				#endif
				return;
			}	
			hfnet_udp_sendto(socket, lan_buf, lan_buf_len, peeraddr, peerport);
			#if defined(_sys_debug_)
				u_printf("DEVICE_TYPE=======%s,lan_buf=======%s,lan_buf_len=====%d\n",DEVICE_TYPE,lan_buf,lan_buf_len);
		    #endif
		 break;
		 default : break;   
	}	
}

char cmd_buff[128]={0};

/// <summary>
/// 设置AP名称
/// </summary>
int set_ap_name(void)
{
	char *words[6]={NULL};
	char rsp[64]={0};
	char ap_name[32];
	char buff_set_name[64]={0};
    memset(ap_name,0x00,sizeof(ap_name));
	// strcat(ap_name,"HF-LPT120<MAC:") ;
	// strcat(ap_name,get_mac) ;	
	// strcat(ap_name,">") ;	
	strcat(ap_name,"HF-LPT120") ;	
	hfat_send_cmd("AT+WAP\r\n",sizeof("AT+WAP\r\n"),rsp,64);
	if(hfat_get_words(rsp,words, 6)>0)
	{
		if((0==strncmp(words[0],"+ok",strlen("+ok"))))
		{
		  #if defined(_sys_debug_)
		     u_printf("ap_name==========%s\n", words[2]);
          #endif  	
		  if(strstr(ap_name,words[2])==NULL)
		  {
			    strcat(buff_set_name,"AT+WAP=11BGN,");
			    strcat(buff_set_name,ap_name);	
			    strcat(buff_set_name,",AUTO\r\n");			

             	hfat_send_cmd(buff_set_name,strlen(buff_set_name),rsp,64);  
                if(hfat_get_words(rsp,words, 3)>0)
				{
					if((0==strncmp(words[0],"+ok",strlen("+ok"))))
					{
						if(set_work_mode_ap()==0)
						{
                           	hfat_send_cmd("AT+CFGTF\r\n",sizeof("AT+CFGTF\r\n"),rsp,64); 
							return 0;
						}
						//hfat_send_cmd("AT+FCLR\r\n",sizeof("AT+FCLR\r\n"),rsp,64);  
						
					}
				}
		  }
		  return 2;
		}
	}	
	return -1;	
}
/// <summary>
/// 链接到路由器
/// </summary>
int connect_to_router(char *a,char *b,char *c,char *d)
{
	char *words[6]={NULL};
	char *cmd1="AT+WSSSID=";
	char *cmd2="AT+WSKEY=";	
	char *dot=",";	
	char *tail="\r\n";	

	char rsp[64]={0};

	memset(cmd_buff,0x00,sizeof(cmd_buff));
	strncat(cmd_buff,cmd1,strlen(cmd1));
	strncat(cmd_buff,a,strlen(a));	
	strncat(cmd_buff,tail,strlen(tail));		
	#if defined(_sys_debug_)
		u_printf("cmd_buff1=========================%s,%d\n",cmd_buff,strlen(cmd_buff));
    #endif  
	hfat_send_cmd(cmd_buff,strlen(cmd_buff),rsp,64);
    
	if(hfat_get_words(rsp,words, 2)>0)
	{
		#if defined(_sys_debug_)
		  u_printf("cmd_buff1 hfat_get_words ok===%s\n",words[0]);
        #endif  
		if(0!=strncmp(words[0],"+ok",strlen("+ok")))
		{		  	
		  return -1;
		}
	}	
	memset(rsp,0x00,sizeof(rsp));
	memset(cmd_buff,0x00,sizeof(cmd_buff));
	strncat(cmd_buff,cmd2,strlen(cmd2));
	strncat(cmd_buff,"WPA2PSK",strlen("WPA2PSK"));
	strncat(cmd_buff,dot,strlen(dot));		
	strncat(cmd_buff,"AES",strlen("AES"));	
	strncat(cmd_buff,dot,strlen(dot));		
	strncat(cmd_buff,d,strlen(d));			
	strncat(cmd_buff,tail,strlen(tail));
	#if defined(_sys_debug_)
	u_printf("cmd_buff2=========================%s,%d\n",cmd_buff,strlen(cmd_buff));
    #endif  
	hfat_send_cmd(cmd_buff,strlen(cmd_buff),rsp,64);

	if(hfat_get_words(rsp,words, 2)>0)
	{
		if(0!=strncmp(words[0],"+ok",strlen("+ok")))
		{
		  return -1;
		}
	}	
 
	if(0!=set_work_mode())
	{
		return -1;
	}

    return 0;	
}



char udp_data_para[256];
char sta_ap_para[2][64];
char *ret_mac_pre="<<mac=";
char ret_mac[64];
int ret_udp=0;
int loop_udp=0;
static int app_udp_recv_callback_report(NETSOCKET socket, 
	unsigned char *data, 
	unsigned short len,
	uip_ipaddr_t *peeraddr, 
	unsigned short peerport)
{	
  memset(sta_ap_para[0],0x00,64);
  memset(sta_ap_para[1],0x00,64); 
  memset(udp_data_para,0x00,sizeof(udp_data_para));
  memcpy(udp_data_para,data,len);

  ret_udp=sscanf(udp_data_para,">>%s%s",&sta_ap_para[0],&sta_ap_para[1]);
  #if defined(_sys_debug_)
	u_printf("udp_data=========================%s\n",udp_data_para);
	u_printf("udp_data=========================%d\n",ret_udp);
  #endif  
  if(ret_udp==2)
  {
	if(0==connect_to_router(sta_ap_para[0],NULL,NULL,sta_ap_para[1]))
	{   
		if(0==get_sta_mac());
		{
			#if defined(_sys_debug_)
				u_printf("get_sta_mac ok=========================%s\n");		
	        #endif 
		}			
		memset(ret_mac,0x00,64);

		//  for(loop_udp=0;loop_udp<12;loop_udp++)	ret_mac[loop_udp]=ret_mac_pre[loop_udp];
		 memcpy(ret_mac,ret_mac_pre,6);
		 memcpy(ret_mac+6,get_mac,12);		 
	

		#if defined(_sys_debug_)
			u_printf("ret_mac_pre=========================%s\n",ret_mac_pre);
			u_printf("get_mac=========================%s\n",get_mac);
			u_printf("ret_mac=========================%s\n",ret_mac);	
			u_printf("ret_mac=========================");	
			for(loop_udp=0;loop_udp<sizeof(ret_mac);loop_udp++)  u_printf("%02x ",ret_mac[loop_udp]); 
			u_printf("\r\n");					
	    #endif  
		if(HF_SUCCESS==hfnet_udp_sendto(socket, ret_mac, 24, peeraddr, peerport))
		{
		   if(HF_SUCCESS==hfnet_udp_sendto(socket, ret_mac, 24, peeraddr, peerport))
		   hfsys_reset();
		}		
	}
	else
	{
		if(HF_SUCCESS==hfnet_udp_sendto(socket, "<<set failed\n", strlen("<<set failed\n"), peeraddr, peerport))
		{
		   hfnet_udp_sendto(socket, "<<set failed\n", strlen("<<set failed\n"), peeraddr, peerport);
		   hfsys_reset();
		}
	}
  }
  
  hfuart_send(HFUART0,data,len,100);

  return len;
}




void udp_airkiss_start(void)
{
	struct udp_socket u_socket;
	u_socket.l_port = 12476;
	u_socket.recv_callback = app_udp_recv_callback;
	u_socket.recv_data_maxlen = 1024;
	app_udp_socket = hfnet_udp_create(&u_socket);
	if(app_udp_socket>=0)
	{
		#if defined(_sys_debug_)
		   u_printf("udp_airkiss_start OK\n");
	    #endif	
	}
	else
	{
		#if defined(_sys_debug_)
			u_printf("udp_airkiss_start FAIL\n");
	    #endif		
	}
	
}

void udp_report_start(void)
{	
	struct udp_socket u_socket;	

	u_socket.l_port = 20001;
	u_socket.recv_callback = app_udp_recv_callback_report;
	u_socket.recv_data_maxlen = 1024;	
	u_socket_report = hfnet_udp_create(&u_socket);
	if(u_socket_report<0)
	{
		#if defined(_sys_debug_)
			u_printf("udp_report_start FAIL\n");
	    #endif	
	}
	else 
	{
		#if defined(_sys_debug_)
			u_printf("udp_report_start OK\n");
	    #endif		   
	}
}

// static int USER_FUNC socketa_recv_callback(uint32_t event,char *data,uint32_t len,uint32_t buf_len)
// {
// 	if(event==HFNET_SOCKETA_CONNECTED)
// 	{
		
// 	}
// 	else if(event==HFNET_SOCKETA_DISCONNECTED)
// 	{
	
// 	}
// 	else if(event==HFNET_SOCKETA_DATA_READY)
// 	{
// 		#if defined(_sys_debug_)
// 			u_printf("recv_data===============%s\n",data);
//         #endif
// 	}
// 	return 0;
// }
char rsp[64]={0x00};

int USER_FUNC  app_main(void)
{
	#if defined(_sys_debug_)
		hfat_send_cmd("AT+NDBGL=3,1\r\n",sizeof("T+NDBGL=3,1\r\n"),rsp,64); 
    #endif
    //hfat_send_cmd("AT+NDBGL=0,1\r\n",sizeof("T+NDBGL=0,1\r\n"),rsp,64); 	
	//hfat_send_cmd("AT+NDBGL=0\r\n",sizeof("AT+NDBGL=0\r\n"),rsp,64); 	 
	//u_printf("\n%s Start %s %s\n\n",g_hfm_name[__HF_MODULE_ID__],__DATE__,__TIME__);
	if(hfnet_start_assis(ASSIS_PORT) != HF_SUCCESS)
	{
		//HF_Debug(DEBUG_WARN,"start httpd fail\n");
	}
#ifdef SUPPORT_UART_THROUGH

	if(hfnet_start_socketa(0,(hfnet_callback_t)socketa_recv_callback) != HF_SUCCESS)
	{
		//HF_Debug(DEBUG_WARN,"start socketa fail\n");
	}
	if(hfnet_start_uart_ex(0,uart_recv_callback,4096) != HF_SUCCESS)
	{
		//HF_Debug(DEBUG_WARN,"start uart fail!\n");
	}
#else
	if(hfnet_start_socketa(0,(hfnet_callback_t)socketa_recv_callback) != HF_SUCCESS)
	{
		//HF_Debug(DEBUG_WARN,"start socketa fail\n");
	}
	
	#if defined(_BANTRATE9600_)
		hfat_send_cmd("AT+UART=9600,8,1,NONE,NFC\r\n",sizeof("AT+UART=9600,8,1,NONE,NFC\r\n"),rsp,64); 
    #endif

	if(hfnet_start_uart_ex(0,uart_recv_callback,1024) != HF_SUCCESS)
	{
		//HF_Debug(DEBUG_WARN,"start uart fail!\n");
	}
#endif
    if(hfsys_register_system_event((hfsys_event_callback_t)hfsys_event_callback) != HF_SUCCESS)
    {
        //u_printf("register system event fail\n");
    }
	test_timer_start();
	udp_airkiss_start();
	udp_report_start();	
	tcpserver_test_start();
	if(0==get_sta_mac());
	// if(0==set_ap_name())
	// {
    //   	#if defined(_sys_debug_)
    //         u_printf("AP NAME ok\n");
    //     #endif
	// }
	// else
	// {
    //   	#if defined(_sys_debug_)
    //         u_printf("AP NAME HAS BEEN ok\n");
    //     #endif		
	// }	
	return 1;
}



